<?php

/**
 * Example Controller
 *
 * PHP version 5
 *
 * @category Controller
 * @package  Croogo
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class CountriesController extends AppController {

/**
 * Controller name
 *
 * @var string
 * @access public
 */
	public $name = 'Countries';

/**
 * Components
 *
 * @var array
 * @access public
 */
	public $components = array(
		'Search.Prg',
	);

/**
 * Preset Variables Search
 *
 * @var array
 * @access public
 */
	public $presetVars = array(
		'name' => array('type' => 'value'),
	);
	
	
/**
 * Models used by the Controller
 *
 * @var array
 * @access public
 */
	public $uses = array('Countries.Country');
	
	public $limit_countries = array();	

/**
 * beforeFilter
 *
 * @return void
 * @access public
 */
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Security->unlockedActions = array('admin_toggle');
		if(in_array($this->request->action,array('admin_index'))){
			$this->limit_countries = json_decode(Configure::read('Countries.country'));			
		}		
	}

/**
 * Toggle Countries status
 *
 * @param $id string Country id
 * @param $status integer Current Country status
 * @return void
 */
	public function admin_toggle($id = null, $status = null) {
		$this->Croogo->fieldToggle($this->Country, $id, $status);
	}	
	
/**
 * admin_index
 *
 * @return void
 */
	public function admin_index() {
		$this->set('title_for_layout', __('Countries'));
		$this->Prg->commonProcess();
		$searchFields = array('name');
		
		$this->Country->recursive = 0;
		$this->paginate['Country']['order'] = 'Country.weight ASC';
		$this->paginate['Country']['conditions']['Country.id'] = $this->limit_countries;
		$this->set('countries', $this->paginate($this->Country->parseCriteria($this->passedArgs)));
		$this->set('searchFields', $searchFields);
	}
	
/**
 * Admin add
 *
 * @return void
 * @access public
 */
	public function admin_add() {
		$this->set('title_for_layout', __('Add Country'));

		if (!empty($this->request->data)) {
			$this->Country->create();
			if ($this->Country->save($this->request->data)) {
				$this->Session->setFlash(__('The Country has been saved'), 'default', array('class' => 'success'));
				$this->Croogo->redirect(array('action' => 'edit', $this->Country->id));
			} else {
				$this->Session->setFlash(__('The Country could not be saved. Please, try again.'), 'default', array('class' => 'error'));
			}
		}
	}

/**
 * Admin edit
 *
 * @param integer $id
 * @return void
 * @access public
 */
	public function admin_edit($id = null) {
		$this->set('title_for_layout', __('Edit Country'));

		if (!$id && empty($this->request->data)) {
			$this->Session->setFlash(__('Invalid Country'), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->request->data)) {
			if ($this->Country->save($this->request->data)) {
				$this->Session->setFlash(__('The Country has been saved'), 'default', array('class' => 'success'));
				$this->Croogo->redirect(array('action' => 'edit', $this->Country->id));
			} else {
				$this->Session->setFlash(__('The Country could not be saved. Please, try again.'), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->request->data)) {
			$this->request->data = $this->Country->read(null, $id);
		}
	}	
	
/**
 * Admin delete
 *
 * @param integer $id
 * @return void
 * @access public
 */
	public function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Country'), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->Country->delete($id)) {
			$this->Session->setFlash(__('Country deleted'), 'default', array('class' => 'success'));
			$this->redirect(array('action' => 'index'));
		}
	}

/**
 * Admin moveup
 *
 * @param integer $id
 * @param integer $step
 * @return void
 * @access public
 */
	public function admin_moveup($id, $step = 1) {
		if ($this->Country->moveUp($id, $step)) {
			$this->Session->setFlash(__('Moved up successfully'), 'default', array('class' => 'success'));
		} else {
			$this->Session->setFlash(__('Could not move up'), 'default', array('class' => 'error'));
		}

		$this->redirect(array('action' => 'index'));
	}

/**
 * Admin movedown
 *
 * @param integer $id
 * @param integer $step
 * @return void
 * @access public
 */
	public function admin_movedown($id, $step = 1) {
		if ($this->Country->moveDown($id, $step)) {
			$this->Session->setFlash(__('Moved down successfully'), 'default', array('class' => 'success'));
		} else {
			$this->Session->setFlash(__('Could not move down'), 'default', array('class' => 'error'));
		}

		$this->redirect(array('action' => 'index'));
	}	

/**
 * Admin process
 *
 * @return void
 * @access public
 */
	public function admin_process() {
		$action = $this->request->data['Country']['action'];
		$ids = array();
		foreach ($this->request->data['Country'] as $id => $value) {
			if ($id != 'action' && $value['id'] == 1) {
				$ids[] = $id;
			}
		}

		if (count($ids) == 0 || $action == null) {
			$this->Session->setFlash(__('No items selected.'), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}

		if ($action == 'delete' &&
			$this->Country->deleteAll(array('Country.id' => $ids), true, true)) {
			$this->Session->setFlash(__('Country deleted'), 'default', array('class' => 'success'));
		} elseif ($action == 'publish' &&
			$this->Country->updateAll(array('Country.status' => true), array('Country.id' => $ids))) {
			$this->Session->setFlash(__('Countries published'), 'default', array('class' => 'success'));
		} elseif ($action == 'unpublish' &&
			$this->Country->updateAll(array('Country.status' => false), array('Country.id' => $ids))) {
			$this->Session->setFlash(__('Countries unpublished'), 'default', array('class' => 'success'));
		} else {
			$this->Session->setFlash(__('An error occurred.'), 'default', array('class' => 'error'));
		}

		$this->redirect(array('action' => 'index'));
	}	
	
/**
 * index
 *
 * @return void
 */
	public function index() {
		$this->set('title_for_layout', __('Countries'));
		$this->set('exampleVariable', 'value here');
	}
}
